package pimp.organizermanager;

import pimp.*;
import java.util.*;
import javax.swing.tree.TreeModel;

public class CalendarGroup {

    private HashMap<Integer, Priority> priorityMap;
    private LabelTree labelTree = null; //inicjalizacja, przy dodaniu pierwszego node'a
    final private StandardCalendar standardCalendar = StandardCalendar.getInstance();
    final private CyclicCalendar cyclicCalendar = CyclicCalendar.getInstance();

    private Calendar currentDate;

    private CalendarGroup() {
        Date currentDate = this.getCurrentDate();
        System.out.println("Today's date is:" + currentDate.getThisYear() +
                "-" + (currentDate.getThisMonth() + 1) + "-" +
                currentDate.getThisDay());
        System.out.println("CalendarGroup initialization completed!");
    }

    private static class Singleton {
        private final static CalendarGroup instance = new CalendarGroup();
    }

    public static CalendarGroup getInstance() {
        return Singleton.instance;
    }

    public Date getCurrentDate() {
        currentDate = Calendar.getInstance();

        int year = currentDate.get(Calendar.YEAR);
        int month = currentDate.get(Calendar.MONTH);
        int day = currentDate.get(Calendar.DAY_OF_MONTH);

        return new Date(year, month, day);
    }

    public Map<ID ,GlobalEvent> getGlobalEventByDate(Date beginDate, Date endDate) {
         System.out.println("2.");
        beginDate.printDate();
        endDate.printDate();
        Map<ID, GlobalEvent> result = new HashMap<ID, GlobalEvent>();
        result.putAll(standardCalendar.getGlobalEventByDate(beginDate, endDate));
        result.putAll(cyclicCalendar.getGlobalEventByDate(beginDate, endDate));
        return result;
    }

    public Map<ID, GlobalEvent> getGlobalEventByDate(Date beginDate, Date endDate,
            ID id) {
        Map<ID, GlobalEvent> result = new HashMap<ID, GlobalEvent>();
        result.putAll(standardCalendar.getGlobalEventByDate(beginDate, endDate, id));
        result.putAll(cyclicCalendar.getGlobalEventByDate(beginDate, endDate, id));
        return result;
    }

    public boolean addEvent(Event event) {
        Date beginDate = event.getBeginDate();
        Date endDate = event.getEndDate();
        if(standardCalendar.addEvent(event, beginDate, endDate)) {
            return true;
        }
        return false;
    }

    public boolean addCycle(Cycle cycle) {
        Vector<Date> dates = cycle.getBeginDates();
        Date endDate = cycle.getFinishDate();

        for(int i = 0; i < dates.size(); ++i) {
            if(OurCalendar.calculateDate(dates.get(i)) == false) {
                return false;
            }
        }

        if(OurCalendar.calculateDate(endDate) == false) {
            return false;
        }

        Collections.sort(dates, new Comparer());

        if(dates.size() > 0) {
            System.out.println("I'm not empty. Hello.");
        }
        else {
            System.out.println("FUCK");
        }

        if(cyclicCalendar.addCycle(cycle)) {
            return true;
        }
        else {
            return false;
        }
    }

    void addGlobalEvent(GlobalEvent globalEvent) {

        if(labelTree == null)
        {
            labelTree = new LabelTree(OrganizerManager.getInstance().getCategoriesTreeModel());
        }
        labelTree.addGlobalEvent(globalEvent);
    }

    public TreeModel getLabelTreeModel()
    {
        if(labelTree == null)
        {
            labelTree = new LabelTree(OrganizerManager.getInstance().getCategoriesTreeModel());
        }
        return labelTree.getLabelTreeModel();
    }

}
